//
//  WLWelComeViewController.swift
//  weibo
//
//  Created by 刁 海瑞 on 16/8/12.
//  Copyright © 2016年 毋磊. All rights reserved.
//

import UIKit
import SnapKit

class WLWelComeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    override func loadView() {
        super.loadView()
        view = bgImageView
        
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
//    背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image:UIImage(named: "ad_background"))
//    头像
    private lazy var headImageView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
//        修改圆角
        imageView.layer.cornerRadius = 45
//        剪切多余图片
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
//    欢迎信息
    private lazy var messageLabel: UILabel = {
        
        let label =  UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.text = "欢迎回来"
        
        return label
    }()
    
    private func setupUI() {
        
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(10)
        }

    }
    
//    执行动画
    private func startAnimation() {
        
        messageLabel.alpha = 0
        
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        print("页面加载成功")
                })
        }
    }

}
